package com.webshop.service;

import com.webshop.DataDao.OrderDao;
import com.webshop.JavaBean.Detail_order;
import com.webshop.JavaBean.Order;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class OrderService {
    OrderDao od = new OrderDao();

    /**
     * Service 层的展示订单
     * @param name
     * @return
     */
    public List<Order> showOrder(String name) {

        List<Order> orders = od.showOrder(name);
        for (Order q : orders) {
            q.setDetail_order(od.showDetail(q.getOrderID()));

        }
        return orders;
    }
    /**
     * 已完成订单
     * @param name
     * @return
     * @throws Exception
     */
    public List<Order> finsh(String name)throws Exception{

        List<Order> orders=new ArrayList<>();
        orders=od.finsh(name);
        for (Order q : orders) {
            q.setDetail_order(od.showDetail(q.getOrderID()));
        }
        return  orders;
    }

    /**
     * 为完成订单
     * @param name
     * @return
     * @throws Exception
     */
    public List<Order> unfinsh(String name)throws Exception{

        List<Order> orders=new ArrayList<>();
        orders=od.unfinsh(name);
        for (Order q : orders) {
            q.setDetail_order(od.showDetail(q.getOrderID()));
        }
        return  orders;
    }

    /**
     * 修改订单状态；
     * @param ID
     */
    public  void update(String ID){
        od.update(ID);
    }


    /**
     * 生成订单编号
     * @param name
     * @return
     */
    public String bornOrder(String name) {
        Calendar calendar = Calendar.getInstance();
        String year = calendar.get(Calendar.YEAR)+"";
        String month = calendar.get(Calendar.MONTH)+1+"";
        String date = calendar.get(Calendar.DATE)+"";
        String hour = calendar.get(Calendar.HOUR_OF_DAY)+"";
        String minutes = calendar.get(Calendar.MINUTE)+"";
        String second = calendar.get(Calendar.SECOND)+"";
        String temp = name.hashCode() + "";
        String ordernum = year + month + date + hour + minutes + second + temp;
        return ordernum;
    }

    /**
     * 新增主订单
     * @param order
     */
    public void addOrder(Order order) {
        od.insertOrder(order);

    }

    /**
     * 新增详细订单
     * @param detail_order
     */

    public void addDetailOrder(Detail_order detail_order) {
        od.addDetailOrder(detail_order);

    }

    /**
     * 返回订单创建时间
     * @return
     */
    public String backTime() {
        Calendar calendar = Calendar.getInstance();
        String year = calendar.get(Calendar.YEAR)+"";
        String month = calendar.get(Calendar.MONTH)+1+"";
        String date = calendar.get(Calendar.DATE)+"";
        String hour = calendar.get(Calendar.HOUR_OF_DAY)+"";
        String minutes = calendar.get(Calendar.MINUTE)+"";
        String second = calendar.get(Calendar.SECOND)+"";
        String time = year + month + date + hour + minutes + second;
        return time;
    }
}

